package igrupobbva.pe.sistemareniec.actions;

import igrupobbva.pe.sistemareniec.forms.BitacoraForm;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.grupobbva.iici.serializable.Bitacora;
import com.grupobbva.iici.serializable.BitacoraDetalle;
import com.grupobbva.iici.serializable.Corporativa;
import com.grupobbva.iici.serializable.Scheduler;
import com.grupobbva.iici.service.BitacoraService;
import com.grupobbva.iici.service.PerfilService;
import com.grupobbva.iici.service.ProcesoService;
import com.grupobbva.iici.service.SchedulerService;
import com.grupobbva.iici.service.impl.ProcesoServiceImpl;
import com.grupobbva.iici.util.ConstantesUtil;
import com.grupobbva.iici.util.FiltroDTO;
import com.grupobbva.iici.util.SpringUtils;

public class BitacoraAction extends DispatchAction {
	
	static Logger logger = Logger.getLogger(BitacoraAction.class
			.getName());
	
	BitacoraService 	bitacoraService		=null;
	SchedulerService 	schedulerService	=null;
	SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd/MM/yyyy");

	public ActionForward inicio(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BitacoraForm bitacoraForm=(BitacoraForm)form;
		schedulerService = SpringUtils.getBean(request, SchedulerService.class);
		Date fecha=new Date();
		fecha.setDate(fecha.getDate()-1);
		bitacoraForm.setFechaInicio(simpleDateFormat.format(fecha));
		bitacoraForm.setFechaFin(simpleDateFormat.format(new Date()));		
		
		List<Scheduler> lstProceso=schedulerService.obtenerEntidadesPorFiltros(Scheduler.class, null);
		request.getSession().setAttribute("procesos", lstProceso);
		bitacoraForm.setCodigoProceso(lstProceso.get(0).getIdScheduler());
		return buscar(mapping, form, request, response);

	}
	public ActionForward buscar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionErrors errors = new ActionErrors();
		bitacoraService  = SpringUtils.getBean(request, BitacoraService.class);
		schedulerService = SpringUtils.getBean(request, SchedulerService.class);
		BitacoraForm bitacoraForm=(BitacoraForm)form;
		ActionForward forward = new ActionForward(); // return value
		try {			
			FiltroDTO filtroDTO=new FiltroDTO();
			filtroDTO.addParametro("codMult", ConstantesUtil.ESTADO_BITACORA_COD);
			request.getSession().setAttribute("estados", schedulerService.obtenerEntidadesPorFiltros(Corporativa.class, filtroDTO));
			Date fechaInicio		= null;
			Date fechaFin			= null;
			String codigoEstado	= null;
			Integer codigoProceso	= null;
			if(bitacoraForm!=null){
				codigoEstado	= bitacoraForm.getCodigoEstado();
				codigoProceso	= bitacoraForm.getCodigoProceso();
				fechaInicio		= simpleDateFormat.parse(bitacoraForm.getFechaInicio());
				fechaFin		= simpleDateFormat.parse(bitacoraForm.getFechaFin());
			}
			
			filtroDTO=new FiltroDTO();
			filtroDTO.addParametro("idEstado", codigoEstado);
			filtroDTO.addParametro("idScheduler", codigoProceso);
			filtroDTO.addParametro("fechaInicio", fechaInicio);
			filtroDTO.addParametro("fechaFin", fechaFin);

			request.getSession().setAttribute("listaBitacora", bitacoraService.buscarBitacora(filtroDTO));
			request.getSession().setAttribute("maestro", true);
			request.getSession().setAttribute("detalle", false);
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));  //$NON-NLS-2$
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("error"); 
		} else {
			forward = mapping.findForward("ok"); 
		}
		// Finish with
		return (forward);

	}
	public ActionForward ejecutar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionErrors errors = new ActionErrors();
		bitacoraService  = SpringUtils.getBean(request, BitacoraService.class);
		schedulerService = SpringUtils.getBean(request, SchedulerService.class);
		ProcesoService procesoService=SpringUtils.getBean(request, ProcesoService.class);
		BitacoraForm bitacoraForm=(BitacoraForm)form;
		ActionForward forward = new ActionForward(); // return value
		SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
		try {			
			Scheduler scheduler=new Scheduler();
			scheduler.setIdScheduler(bitacoraForm.getCodigoProcesoEjecucion());
			Bitacora bitacora=new Bitacora();
			bitacora.setScheduler(scheduler);
			bitacora.setFechaEjecutar(sdf.parse(bitacoraForm.getFechaEjecucion()));
			
			procesoService.realizarCargaArchivoManual(bitacora);
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));  //$NON-NLS-2$
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("error"); 
		} else {
			forward = mapping.findForward("ok"); 
		}
		// Finish with
		return buscar(mapping, bitacoraForm, request, response);

	}
	public ActionForward detalle(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession sesion = request.getSession();
		ActionErrors errors = new ActionErrors();
		int flag =Integer.parseInt((String)sesion.getAttribute("root")) ;
		bitacoraService  = SpringUtils.getBean(request, BitacoraService.class);
		schedulerService = SpringUtils.getBean(request, SchedulerService.class);
		BitacoraForm bitacoraForm=(BitacoraForm)form;
		ActionForward forward = new ActionForward(); // return value
		try {
			List aux=schedulerService.obtenerEntidadesPorFiltros(Scheduler.class, null);
			request.getSession().setAttribute("procesos", aux);
			FiltroDTO filtroDTO=new FiltroDTO();
			filtroDTO.addParametro("bitacora.idBitacora",bitacoraForm.getIdBitacora());
			request.getSession().setAttribute("listaBitacoraDetalle", bitacoraService.obtenerEntidadesPorFiltros(BitacoraDetalle.class, filtroDTO));
			request.getSession().setAttribute("maestro", false);
			request.getSession().setAttribute("detalle", true);
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));  //$NON-NLS-2$
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("error"); 
		} else {
			forward = mapping.findForward("ok"); 
		}
		// Finish with
		return (forward);

	}
}
